cleanup filter class issues. (#215)
authortsteven4 <tsteven4@users.noreply.github.com>
Tue, 3 Jul 2018 17:40:57 +0000 (11:40 -0600)
committerGitHub <noreply@github.com>
Tue, 3 Jul 2018 17:40:57 +0000 (11:40 -0600)
GPSBabel.pro
deprecated/filter_skeleton.cc [new file with mode: 0644]
filter.h
filter_skeleton.cc [deleted file]
route.cc
trackfilter.cc
trackfilter.h

index f6c74520b116198a41e00cdd2560d9743d177db3..e6036e37edbb3b098a8c91fef4b4b73ca93c59e5 100644 (file)
@@ -94,6 +94,7 @@ HEADERS =  \
        csv_util.h \
        defs.h \
        explorist_ini.h \
+       filter.h \
        filterdefs.h \
        garmin_device_xml.h \
        garmin_fs.h \
diff --git a/deprecated/filter_skeleton.cc b/deprecated/filter_skeleton.cc
new file mode 100644 (file)
index 0000000..c05a2a8
--- /dev/null
@@ -0,0 +1,107 @@
+/*
+
+    Filter skeleton:
+
+    Simply copy this file to <your_filter_name>.c and
+    rename all filter_skeleton tokens to <your_filter_name>. Replace
+    the stupid name and address in the Copyright few lines below.
+    To active your new filter you have to create a new section in
+    filter_vecs and finally add complying statements to Makefile.
+
+    Copyright (C) YYYY John Doe, anybody@wherever.com
+    Copyright (C) 2001-YYYY Robert Lipe, robertlipe+source@gpsbabel.org
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
+
+ */
+
+#include "defs.h"
+#include "filterdefs.h"
+
+#define MYNAME "filter_skeleton"
+
+#if FILTERS_ENABLED
+
+// Any arg in this list will appear in command line help and will be
+// populated for you.
+static
+arglist_t filter_skeleton_args[] = {
+// {"foo", &fooopt, "The text of the foo option in help",
+//   "default", ARGYTPE_STRING, ARG_NOMINMAX} ,
+  ARG_TERMINATOR
+};
+
+/*******************************************************************************
+* %%%        global callbacks called by gpsbabel main process              %%% *
+*******************************************************************************/
+
+static void
+filter_skeleton_init(const char* args)
+{
+  /* Called before filter processing */
+
+  /* optional.  If not needed, delete and replace entry in vecs with NULL  */
+
+  /* This may be used to parse filter options, allocate memory, and do other
+   * housekeeping that should be done before filtering */
+}
+
+static void
+filter_skeleton_process()      /* this procedure must be present in vecs */
+{
+// Here is how you register callbacks for all waypoints, routes, tracks.
+// waypt_disp_all(waypt)
+// route_disp_all(head, tail, rtept);
+// track_disp_all(head, tail, trkpt);
+}
+
+static void
+filter_skeleton_deinit()
+{
+  /* called after filter processing */
+
+  /* optional.   If not needed, delete and replace entry in vecs with NULL */
+
+  /* This should be used to clean up any memory allocations that are no longer
+   * needed after the filter terminates. */
+}
+
+static void
+filter_skeleton_exit()
+{
+  /* called on program exit */
+
+  /* optional.   If not needed, delete and replace entry in vecs with NULL */
+
+  /* You should not need this for simple filters, but it may be used to
+   * clean up memory allocations that must persist from one invocation of
+   * your filter to the next (for example, the stack in the stack filter.)
+   * Note that this member will be called even if your filter has not been
+   * used, so it *cannot* assume that _init or _process has been called
+   * previously. */
+}
+
+/*******************************************************************************/
+
+filter_vecs_t filter_skeleton_vecs = {
+  filter_skeleton_init,
+  filter_skeleton_process,
+  filter_skeleton_deinit,
+  filter_skeleton_exit,
+  filter_skeleton_args
+};
+
+/*******************************************************************************/
+#endif // FILTERS_ENABLED
index 20c31d59137288c6b1330e148dae13924fa88d30..6d83e10a5b81a8222872ca571a8cfdf5aaadd7ec 100644 (file)
--- a/filter.h
+++ b/filter.h
 class Filter
 {
 public:
+  Filter() = default;
+  // Provide virtual public destructor to avoid undefined behavior when
+  // an object of derived class type is deleted through a pointer to
+  // its base class type.
+  // https://wiki.sei.cmu.edu/confluence/display/cplusplus/OOP52-CPP.+Do+not+delete+a+polymorphic+object+without+a+virtual+destructor
+  virtual ~Filter() = default;
+  // And that requires us to explictly default the move and copy operations.
+  // https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c21-if-you-define-or-delete-any-default-operation-define-or-delete-them-all.
+  Filter(const Filter&) = default;
+  Filter& operator=(const Filter&) = default;
+  Filter(Filter&&) = default;
+  Filter& operator=(Filter&&) = default;
+
   virtual arglist_t* get_args() = 0;
 
   virtual void init()
diff --git a/filter_skeleton.cc b/filter_skeleton.cc
deleted file mode 100644 (file)
index c05a2a8..0000000
+++ /dev/null
@@ -1,107 +0,0 @@
-/*
-
-    Filter skeleton:
-
-    Simply copy this file to <your_filter_name>.c and
-    rename all filter_skeleton tokens to <your_filter_name>. Replace
-    the stupid name and address in the Copyright few lines below.
-    To active your new filter you have to create a new section in
-    filter_vecs and finally add complying statements to Makefile.
-
-    Copyright (C) YYYY John Doe, anybody@wherever.com
-    Copyright (C) 2001-YYYY Robert Lipe, robertlipe+source@gpsbabel.org
-
-    This program is free software; you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation; either version 2 of the License, or
-    (at your option) any later version.
-
-    This program is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with this program; if not, write to the Free Software
-    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
-
- */
-
-#include "defs.h"
-#include "filterdefs.h"
-
-#define MYNAME "filter_skeleton"
-
-#if FILTERS_ENABLED
-
-// Any arg in this list will appear in command line help and will be
-// populated for you.
-static
-arglist_t filter_skeleton_args[] = {
-// {"foo", &fooopt, "The text of the foo option in help",
-//   "default", ARGYTPE_STRING, ARG_NOMINMAX} ,
-  ARG_TERMINATOR
-};
-
-/*******************************************************************************
-* %%%        global callbacks called by gpsbabel main process              %%% *
-*******************************************************************************/
-
-static void
-filter_skeleton_init(const char* args)
-{
-  /* Called before filter processing */
-
-  /* optional.  If not needed, delete and replace entry in vecs with NULL  */
-
-  /* This may be used to parse filter options, allocate memory, and do other
-   * housekeeping that should be done before filtering */
-}
-
-static void
-filter_skeleton_process()      /* this procedure must be present in vecs */
-{
-// Here is how you register callbacks for all waypoints, routes, tracks.
-// waypt_disp_all(waypt)
-// route_disp_all(head, tail, rtept);
-// track_disp_all(head, tail, trkpt);
-}
-
-static void
-filter_skeleton_deinit()
-{
-  /* called after filter processing */
-
-  /* optional.   If not needed, delete and replace entry in vecs with NULL */
-
-  /* This should be used to clean up any memory allocations that are no longer
-   * needed after the filter terminates. */
-}
-
-static void
-filter_skeleton_exit()
-{
-  /* called on program exit */
-
-  /* optional.   If not needed, delete and replace entry in vecs with NULL */
-
-  /* You should not need this for simple filters, but it may be used to
-   * clean up memory allocations that must persist from one invocation of
-   * your filter to the next (for example, the stack in the stack filter.)
-   * Note that this member will be called even if your filter has not been
-   * used, so it *cannot* assume that _init or _process has been called
-   * previously. */
-}
-
-/*******************************************************************************/
-
-filter_vecs_t filter_skeleton_vecs = {
-  filter_skeleton_init,
-  filter_skeleton_process,
-  filter_skeleton_deinit,
-  filter_skeleton_exit,
-  filter_skeleton_args
-};
-
-/*******************************************************************************/
-#endif // FILTERS_ENABLED
index cc440fa849a1a358c9fb5b9b431b5bb088a99b6c..21c7d10677538eebc2f7de03c826b64d0e19ee6b 100644 (file)
--- a/route.cc
+++ b/route.cc
@@ -254,7 +254,7 @@ track_del_wpt(route_head* rte, Waypoint* wpt)
 }
 
 void
-route_disp(const route_head* rh, std::nullptr_t /* wc */)
+route_disp(const route_head* /* rh */, std::nullptr_t /* wc */)
 {
 // wc == nullptr
 }
index df783cd7fcf2ceb170cd46d43e8418379e88e9ca..a10432700db0def9b939c93c8b06f589de36e85e 100644 (file)
@@ -29,6 +29,7 @@
 #include "xmlgeneric.h"
 #include <QtCore/QRegExp>
 #include <QtCore/QXmlStreamAttributes>
+#include <cassert>
 #include <cmath>
 #include <cstdio> /* for snprintf */
 #include <cstdlib> /* for qsort */
@@ -555,13 +556,12 @@ void TrackFilter::trackfilter_split()
     int new_track_flag;
 
     if ((opt_interval == 0) && (opt_distance == 0)) {
-      struct tm t1, t2;
 // FIXME: This whole function needs to be reconsidered for arbitrary time.
       time_t tt1 = buff[i]->GetCreationTime().toTime_t();
       time_t tt2 = buff[j]->GetCreationTime().toTime_t();
 
-      t1 = *localtime(&tt1);
-      t2 = *localtime(&tt2);
+      const struct tm t1 = *localtime(&tt1);
+      const struct tm t2 = *localtime(&tt2);
 
       new_track_flag = ((t1.tm_year != t2.tm_year) || (t1.tm_mon != t2.tm_mon) ||
                         (t1.tm_mday != t2.tm_mday));
@@ -917,9 +917,9 @@ TrackFilter::faketime_t TrackFilter::trackfilter_faketime_check(const char* time
   char c;
   const char* cin;
   struct tm time;
-  int timeparse = 1;
+  bool timeparse = true;
   faketime_t result;
-  result.force = 0;
+  result.force = false;
 
   i = j = 0;
   strncpy(fmtstart, "00000101000000", sizeof(fmtstart));
@@ -928,7 +928,7 @@ TrackFilter::faketime_t TrackFilter::trackfilter_faketime_check(const char* time
 
   while ((c = *cin++)) {
     if (c=='f') {
-      result.force = 1;
+      result.force = true;
       continue;
     }
 
@@ -939,7 +939,7 @@ TrackFilter::faketime_t TrackFilter::trackfilter_faketime_check(const char* time
     if (timeparse) {
       if (c == '+') {
         fmtstart[i++] = '\0';
-        timeparse = 0;
+        timeparse = false;
       } else {
         if (fmtstart[i] == '\0') {
           fatal(MYNAME "-faketime: parameter too long \"%s\"!\n", timestr);
@@ -965,33 +965,25 @@ TrackFilter::faketime_t TrackFilter::trackfilter_faketime_check(const char* time
   return result;
 }
 
-int TrackFilter::trackfilter_faketime()             /* returns number of track points left after filtering */
+void TrackFilter::trackfilter_faketime()
 {
-  faketime_t faketime;
-
   queue* elem, *tmp;
-  int i, dropped, inside = 0;
-
-  if (opt_faketime != nullptr) {
-    faketime = trackfilter_faketime_check(opt_faketime);
-  }
 
-  dropped = inside = 0;
+  assert(opt_faketime != nullptr);
+  faketime_t faketime = trackfilter_faketime_check(opt_faketime);
 
-  for (i = 0; i < track_ct; i++) {
+  for (int i = 0; i < track_ct; i++) {
     route_head* track = track_list[i].track;
 
     QUEUE_FOR_EACH((queue*)&track->waypoint_list, elem, tmp) {
       Waypoint* wpt = (Waypoint*)elem;
 
-      if (opt_faketime != nullptr && (!wpt->creation_time.isValid() || faketime.force)) {
+      if (!wpt->creation_time.isValid() || faketime.force) {
         wpt->creation_time = QDateTime::fromTime_t(faketime.start);
         faketime.start += faketime.step;
       }
     }
   }
-
-  return track_pts - dropped;
 }
 
 int TrackFilter::trackfilter_points_are_same(const Waypoint* wpta, const Waypoint* wptb)
index 99a6c88b329794506eac79362a6d9a8c40695575..7c0ca79e74f7a636fde137e617a284da20166d1f 100644 (file)
@@ -216,11 +216,11 @@ private:
   typedef struct faketime_s {
     time_t start;
     int    step;
-    int   force;
+    bool   force;
   } faketime_t;
 
   faketime_t trackfilter_faketime_check(const char* timestr);
-  int trackfilter_faketime();             /* returns number of track points left after filtering */
+  void trackfilter_faketime();             /* returns number of track points left after filtering */
   int trackfilter_points_are_same(const Waypoint* wpta, const Waypoint* wptb);
 
   void trackfilter_segment_head(const route_head* rte);